const user = {
  namespaced: true,
  state: ()=> ({
    address: JSON.parse(uni.getStorageSync('address') || '{}'),
    token: uni.getStorageSync('token') || '',// 用户登录后的token
    userInfo: JSON.parse(uni.getStorageSync('userInfo') || '{}')
  }),
  mutations:{
    // 更新收获地址
    updateAddress(state, address){
      state.address = address
      this.commit('user/saveAddressStorage')
    },
    // 更新用户信息
    updateUserInfo(state, userInfo){
      state.userInfo = userInfo
      this.commit('user/saveUserInfoStorage')
    },
    // 更新 token 字符串
    updateToken(state, token){
      state.token = token
      this.commit('user/saveTokenStorage')
    },
    // 持久化地址
    saveAddressStorage(state,payload){
      uni.setStorageSync('address', JSON.stringify(state.address))
    },
    // 持久化用户信息
    saveUserInfoStorage(state,payload){
      uni.setStorageSync('userInfo', JSON.stringify(state.userInfo))
    },
    // 持久化token
    saveTokenStorage(state,payload){
      uni.setStorageSync('token', JSON.stringify(state.token))
    }
  },
  actions: {
    
  },
  getters: {
    addstr(state){
      if(!state.address.provinceName) return ''
      return state.address.provinceName +  state.address.cityName + state.address.countyName + state.address.detailInfo
    }
  }
}
export default user